﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Workflow.ComponentModel;
using System.ComponentModel;
using System.Diagnostics;

namespace TextFlow.Activities
{
    public class TextFlowCondition : ActivityCondition
    {
        public Expression EvalExpr { set; get; }    // can't serialize dynamic delegates, else we'd just compile this once and store the delegate itself...

        public override bool Evaluate( Activity activity, IServiceProvider provider )
        {
            Debug.Assert( EvalExpr != null );

            ServiceLookupDelegate svclookup = delegate( Type t )
            {
                if ( t == typeof( TextFlowWorkflow ) )
                {
                    return activity.GetActivityByName( "MainFlow", false );
                }
                else
                {
                    return provider.GetService( t );
                }
            };

            return (bool) Utils.ComputeResult( EvalExpr, svclookup );
        }
    }
}
